package cn.tedu.person.service.impl;

import cn.tedu.person.ex.ServiceException;
import cn.tedu.person.mapper.PersonMapper;
import cn.tedu.person.pojo.dto.PersonInsertDTO;
import cn.tedu.person.pojo.dto.PersonUpdateDTO;
import cn.tedu.person.pojo.entity.Person;
import cn.tedu.person.pojo.vo.PersonIndexVO;
import cn.tedu.person.service.IPersonService;
import cn.tedu.person.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PersonServiceImpl implements IPersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public void addNew(PersonInsertDTO personInsertDTO) {
        System.out.println("DTO="+personInsertDTO);

        String name=personInsertDTO.getName();
        int countByName=personMapper.countByName(name);
        if(countByName>0){
            String message="添加失败，名称已经被占用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //先new一个person对象，因为插入的时候需要插入person对象，不能直接插入dto对象
        Person p=new Person();
        //把dto里面的信息复制到c中，没有信息的属性自己添加
        BeanUtils.copyProperties(personInsertDTO,p);
        p.setCreated(new Date());
        personMapper.insert(p);
    }

    @Override
    public List<PersonIndexVO> list() {
        return personMapper.list();
    }

    @Override
    public void delete(Long id) {
        PersonIndexVO queryResult = personMapper.getStandardById(id);
        System.out.println(id);
        if(queryResult==null){
            String message="删除失败，找不到数据";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = personMapper.deleteById(id);
        System.out.println(rows);
        if (rows != 1) {
            String message = "删除相册失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, PersonUpdateDTO personUpdateDTO) {
        PersonIndexVO queryResult = personMapper.getStandardById(id);
        if(queryResult == null){
            String message="修改失败，找不到数据";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = personUpdateDTO.getName();
        int count = personMapper.countByNameAndNotId(id, name);
        if (count > 0) {
            String message = "修改详情失败，名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        Person person=new Person();
        BeanUtils.copyProperties(personUpdateDTO,person);
        person.setId(id);
        int rows=personMapper.update(person);
        if (rows != 1) {
            String message = "修改详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public String selectUrlById(Long id) {
        return personMapper.selectUrlById(id);
    }


    @Override
    public PersonIndexVO getStandardById(Long id) {
        PersonIndexVO queryResult = personMapper.getStandardById(id);
        if(queryResult == null){
            String message="获取数据失败，找不到数据";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<PersonIndexVO> selectByWd(String wd) {
        return personMapper.selectByWd(wd);
    }

}